package com.springioc.ui;

import com.lpz.service.IAccountService;
import com.springioc.dao.IAccountDao;
import com.springioc.service.AccountImpl;
import com.springioc.service.IAccount;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.applet.AppletContext;

public class client {
    /**
     * 获取spring的ioc核心容器，并根据id获取对象
     * @param args
     */
    public static void main(String[] args) {
        //1获取核心容器对象
        /**
         * ApplicationContext的三个常用实现类：
         *      ClassPathxmlApplicationContext 加载类配置文件必须是类路径下。
         *      FileSystemXmlApplicationContext 加载的文件必须是磁盘任意路径（要有权限）的
         *      AnnotationConfigApplicationContext 创建的容器必须是注解。
         */
        //立即加载的机制  读取完配置文件，立即创建对象
        ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
        //ApplicationContext ac=new FileSystemXmlApplicationContext("F:\\two_code\\ssm\\day01_spring\\src\\main\\resources\\bean.xml");
        //2根据id获取对象
        IAccount iAccount=(IAccount)ac.getBean("accountService");//拿到的object的类型通过IAccount接口的类型来强转
        IAccountDao accountDao=ac.getBean("accountDao",IAccountDao.class);//通过传入字节码文件，让其字节码进行强转
        System.out.println(iAccount);
        System.out.println(accountDao);
         iAccount.saveAccount();
         //延迟加载的机制 随着id的创建才开始创建对象。
     /*   Resource resource=new ClassPathResource("bean.xml");
        BeanFactory factory=new XmlBeanFactory(resource);
        IAccount as=(IAccount)factory.getBean("accountService");*/
    }
}
